<template>
  <basic-container>
    <div class="edit-pass-container">
      <el-page-header @back="toBack" :content="'修改密码'" class="return-page"></el-page-header>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="输入原密码" prop="oldPassword">
          <el-input v-model="ruleForm.oldPassword"></el-input>
        </el-form-item>
        <el-form-item label="输入新密码" prop="newPassword1">
          <el-input v-model="ruleForm.newPassword1"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="newPassword2">
          <el-input v-model="ruleForm.newPassword2"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </basic-container>
</template>

<script>
import { updateAccountPassword } from "@/api/user";
export default {
  data() {
    // 密码
    const validatePass = (rule, value, callback) => {
      const passReg = /^.*(?=.{8,16})(?=.*[A-Z])(?=.*[a-z])(?=.*[\d|`~!@#$%^&*? \(\)_\+\[\]\{\}\\\|;:',\./<>\?]).*$/;
      if (!value) {
        callback(new Error('请输入新密码'));
      } else if (!passReg.test(value)) {
        callback(new Error("请输入8-16位，必须包含大小写字母、数字、特殊字符组合的密码。"));
      } else {
        callback()
      }
    };
    // 确认密码
    const validateConfirmPass = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.newPassword1) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        oldPassword: '',
        newPassword1: '',
        newPassword2: '',
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
        ],
        newPassword1: [
          { required: true, validator: validatePass, trigger: 'blur' },
        ],
        newPassword2: [
          { required: true, validator: validateConfirmPass, trigger: 'blur' },
        ],
      }
    }
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = this.ruleForm;8
          updateAccountPassword(params)
            .then((res) => {
              const resData = res.data;
              console.log(120, '更新密码 ', resData);
              this.$message({
                type: "success",
                message: "更新密码成功!"
              });
            })
        } else {
          this.$message.warning('请完善信息!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    toBack() {
      this.$router.go(-1)
    }
  },
};
</script>

<style lang="scss" scoped>
.edit-pass-container {
  color: #fff;

  .return-page {
    margin-bottom: 12px;
  }

  ::v-deep .el-icon-back {
    color: #fff !important;
  }

  ::v-deep .el-page-header__title {
    color: #fff !important;
  }

  ::v-deep .el-page-header__content {
    color: #fff !important;
  }
}
</style>
